/**
* This file is part of OntoApplications (project linked with OntoCompoFramework)
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.test;

import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.objectweb.fractal.adl.ADLException;
import org.objectweb.fractal.adl.Factory;
import org.objectweb.fractal.adl.FactoryFactory;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalContentException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.julia.control.lifecycle.LifeCycleCoordinator;
import org.objectweb.fractal.util.Fractal;

public class Action1VersInput2EtActionSansAffichage {

	ArrayList<Component> componentsToAdd;
	private boolean cancelAppAutoClose;
	
	public Action1VersInput2EtActionSansAffichage(boolean cancelAppAutoClose) {
		this.componentsToAdd = new ArrayList<Component>();
		this.cancelAppAutoClose = cancelAppAutoClose;
	}
	
	public void perform() {
		try {
			Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
			
			//Lancement InsuranceCard Application
			Component trimuicomponent = (Component) f.newComponent("applications.trimstring.ui.TrimStringApp", null);
			
			((LifeCycleCoordinator)trimuicomponent.getFcInterface("lifecycle-controller")).startFc();
			
			ontocompo.api.UIJFrameService trimJframe = (ontocompo.api.UIJFrameService)trimuicomponent.getFcInterface("uijframeservice");
			trimJframe.init();
			JFrame trimjf = trimJframe.getJFrameItSelf();
			
			//Lancement BusinessDirectory Application
			Component reverseuicomponent = (Component) f.newComponent("applications.reversestring.ui.ReverseStringApp", null);
			
			((LifeCycleCoordinator)reverseuicomponent.getFcInterface("lifecycle-controller")).startFc();
			
			ontocompo.api.UIJFrameService reverseJframe = (ontocompo.api.UIJFrameService)reverseuicomponent.getFcInterface("uijframeservice");
			reverseJframe.init();
			JFrame reversejf = reverseJframe.getJFrameItSelf();
			
			if(this.cancelAppAutoClose) {
				trimjf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				reversejf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
			
			Component trimOutReverseInputAndActionComponent = (Component) f.newComponent("ontocompo.test.TrimOutReverseInputAndAction", null);
			
			Fractal.getContentController(trimuicomponent).addFcSubComponent(trimOutReverseInputAndActionComponent);
			
			this.addComponentToAdd(reverseuicomponent);
			
			for(Component c: this.componentsToAdd) {
				Fractal.getContentController(trimuicomponent).addFcSubComponent(c);
			}
			
			Component trimService = this.getComponentFromUid(trimuicomponent,"applications.trimstring.fc.lib.TrimStringServiceImpl");
			Component resultTrimLabel = this.getComponentFromUid(trimuicomponent,"applications.trimstring.ui.lib.TrimStringResultLabel");
			Component reverseInputString = this.getComponentFromUid(reverseuicomponent, "applications.reversestring.ui.lib.ReverseStringStrInput");
			Component reverseButton = this.getComponentFromUid(reverseuicomponent, "applications.reversestring.ui.lib.ReverseStringButton");
			
			ontocompo.api.UIContainerService resultTrimLabelSwingComponent = (ontocompo.api.UIContainerService) resultTrimLabel.getFcInterface("uicontainerservice");
			JComponent resultTrimJL = resultTrimLabelSwingComponent.getComponentItSelf();
			resultTrimJL.setVisible(false);
			
			ontocompo.api.UIContainerService reverseInputStringSwingComponent = (ontocompo.api.UIContainerService) reverseInputString.getFcInterface("uicontainerservice");
			JComponent reverseInputStringJT = reverseInputStringSwingComponent.getComponentItSelf();
			reverseInputStringJT.setVisible(false);
			
			ontocompo.api.UIContainerService reverseButtonSwingComponent = (ontocompo.api.UIContainerService) reverseButton.getFcInterface("uicontainerservice");
			JComponent reverseButtonJB = reverseButtonSwingComponent.getComponentItSelf();
			reverseButtonJB.setVisible(false);
			
			
			((LifeCycleCoordinator)trimService.getFcInterface("lifecycle-controller")).stopFc();
			((LifeCycleCoordinator)reverseInputString.getFcInterface("lifecycle-controller")).stopFc();
			((LifeCycleCoordinator)trimOutReverseInputAndActionComponent.getFcInterface("lifecycle-controller")).stopFc();
			
			BindingController bindingTrimOutReverseInputAndAction = (BindingController)trimOutReverseInputAndActionComponent.getFcInterface("binding-controller");
			
			bindingTrimOutReverseInputAndAction.bindFc("trimstringobservable", trimService.getFcInterface("observable"));
			bindingTrimOutReverseInputAndAction.bindFc("trimservice", trimService.getFcInterface("trimstringservice"));
			bindingTrimOutReverseInputAndAction.bindFc("strinput", reverseInputString.getFcInterface("jTextFieldItf"));
			bindingTrimOutReverseInputAndAction.bindFc("reversebutton", reverseButton.getFcInterface("jButtonItf"));
			
			((LifeCycleCoordinator)trimService.getFcInterface("lifecycle-controller")).startFc();
			((LifeCycleCoordinator)reverseInputString.getFcInterface("lifecycle-controller")).startFc();
			((LifeCycleCoordinator)trimOutReverseInputAndActionComponent.getFcInterface("lifecycle-controller")).startFc();
			
			ontocompo.api.InitComponentItf initItfTrimOutReverseInputAndAction = (ontocompo.api.InitComponentItf) trimOutReverseInputAndActionComponent.getFcInterface("initcomponentitf");
			initItfTrimOutReverseInputAndAction.init();
			
			
		} catch (ADLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalLifeCycleException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchInterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalContentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBindingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private Component getComponentFromUid(Component startComponent, String uid) {
		try {	
			Boolean introspectionOK = false;
			
			Object[] allInterfaces = startComponent.getFcInterfaces();
			for(Object o: allInterfaces) {
				String os = ((Interface) o).getFcItfName();
				if(os.equals("ontocompointrospection")) {
					introspectionOK = true;
				}
			}
			
			if(introspectionOK){
				ontocompo.api.OntoCompoIntrospection introspection;
				
				introspection = (ontocompo.api.OntoCompoIntrospection) startComponent.getFcInterface("ontocompointrospection");
				
				if(introspection != null && introspection.getUid().equals(uid)) {
					return startComponent;
				}
			}
			
			BindingController bindingitf = (BindingController)startComponent.getFcInterface("binding-controller");
			String[] interfaces = bindingitf.listFc();
			
			if(interfaces.length != 0) {
			
				for(String i: interfaces) {
					Object o = bindingitf.lookupFc(i);
					Interface itfserver = (Interface) o;
					Component cserver = itfserver.getFcItfOwner();
					Component result = this.getComponentFromUid(cserver, uid);
					if(result != null) {
						return result;
					}
				}
				
			} else {
				return null;
			}
			
		} catch (NoSuchInterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private void addComponentToAdd(Component startComponent) {
		try {	
			BindingController bindingitf = (BindingController)startComponent.getFcInterface("binding-controller");
			String[] interfaces = bindingitf.listFc();
			
			if(interfaces.length != 0) {
			
				for(String i: interfaces) {
					Object o = bindingitf.lookupFc(i);
					Interface itfserver = (Interface) o;
					Component cserver = itfserver.getFcItfOwner();
					if(!this.componentsToAdd.contains(cserver)) {
						this.componentsToAdd.add(cserver);
					}
					this.addComponentToAdd(cserver);
				}
				
			}
			
		} catch (NoSuchInterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		Action1VersInput2EtActionSansAffichage a1vi2easa = new Action1VersInput2EtActionSansAffichage(false);
		a1vi2easa.perform();
	}
}
